package com.mbigger.admin.controller;

import com.google.common.collect.Maps;
import com.mbigger.admin.beans.PageQuery;
import com.mbigger.admin.beans.PageResult;
import com.mbigger.admin.entity.Acl;
import com.mbigger.admin.entity.Role;
import com.mbigger.admin.entity.User;
import com.mbigger.admin.param.AclParam;
import com.mbigger.admin.service.AclService;
import com.mbigger.admin.service.RoleService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/sys/acl")
public class AclController {

	@Autowired
	private AclService aclService;
	@Autowired
	private RoleService roleService;

	@PostMapping("/save")
	public void saveAclModule(AclParam param) {
		aclService.save(param);
	}

	@PostMapping("/update")
	public void updateAclModule(AclParam param) {
		aclService.update(param);
	}

	@GetMapping("/page")
	public PageResult<Acl> list(Integer aclModuleId, PageQuery pageQuery) {
		return aclService.getPageByAclModuleId(aclModuleId, pageQuery);
	}

	@GetMapping("/acls")
	public Map<String, Object> acls(@RequestParam("aclId") int aclId) {
		Map<String, Object> map = Maps.newHashMap();
		List<Role> roleList = roleService.getRoleListByAclId(aclId);
		List<User> userList = roleService.getUserListByRoleList(roleList);
		map.put("roles", roleList);
		map.put("users", userList);
		return map;
	}
}
